﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoundEdge.Media.Wave;
using SoundEdge.Media.Settings;

namespace SoundEdge.WinControls.Settings
{
    public partial class DirectSoundOutSettingsPanel : UserControl
    {
        private DirectSoundOutSettings _settings;

        public DirectSoundOutSettingsPanel(DirectSoundOutSettings settings)
        {
            _settings = settings;
            InitializeComponent();
            InitialiseDirectSoundControls();
        }

        private void InitialiseDirectSoundControls()
        {
            comboBoxDirectSound.DisplayMember = "Description";
            comboBoxDirectSound.ValueMember = "Guid";
            comboBoxDirectSound.DataSource = DirectSoundOut.Devices;

            DirectSoundDeviceInfo selected = null;
            if (_settings != null)
            {
                selected = comboBoxDirectSound.Items.OfType<DirectSoundDeviceInfo>().FirstOrDefault(item => item.Guid == _settings.SelectedDevice);
            }

            comboBoxDirectSound.SelectedIndex = selected == null ? 0 : comboBoxDirectSound.Items.IndexOf(selected);
        }

        public Guid SelectedDevice 
        {
            get { return (Guid)comboBoxDirectSound.SelectedValue; }
        }

        private void comboBoxDirectSound_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_settings == null || comboBoxDirectSound.SelectedValue == null)
            {
                return;
            }
            _settings.SelectedDevice = (Guid)comboBoxDirectSound.SelectedValue;
        }
    }
}
